package org.apache.portals.tutorials;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;
import java.net.*;
import java.util.ArrayList;
import java.io.*;

import javax.portlet.PortletConfig;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletContext;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.WindowState;
import javax.servlet.RequestDispatcher;
import javax.xml.parsers.ParserConfigurationException;



import org.xml.sax.SAXException;

import model.ParseXML;

public class MyPortlet extends GenericPortlet {

    private static final String ALL_PROJECTS = "/allprojects.jsp";
    private static final String PROJECT_DETAIL = "/projectdetail.jsp";
    private static final String HELP_VIEW = "/help.jsp";

    private PortletRequestDispatcher projectsView;
    private PortletRequestDispatcher projectView;
    private PortletRequestDispatcher helpView;
    private int inc = 1;

    public void doView( RenderRequest request, RenderResponse response )
        throws PortletException, IOException {

        if( WindowState.MINIMIZED.equals( request.getWindowState() ) ) {
        	//dispatch.forward(arg0, arg1)
        	//1java.net.HttpURLConnection http = new HttpURLConnection(new URL("http://localhost:8080/test/ProjectController"));
            return;
        }

        if ( WindowState.NORMAL.equals( request.getWindowState() ) ) {
            //normalView.include( request, response );
        	response.setContentType("text/html; charset=utf-8");
            PrintWriter outPrintWriter = response.getWriter();
            
            //String projectaction = request.getProperty("projectaction");
            //System.out.println("This is project acton"+projectaction);
            if(inc == 1){
	        	URL oracle = new URL("http://localhost:8090/test/ProjectController");
	            URLConnection yc = oracle.openConnection();
	            yc.setDoOutput(true);
	            BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
	            String inputLine = "",xmlString = "";
	            
	            //OutputStreamWriter out = new OutputStreamWriter(yc.getOutputStream());
	            while ((inputLine = in.readLine()) != null){
	            	xmlString += inputLine;
	            }
	            
	            ParseXML pxml = new ParseXML();
	            ArrayList<String> projects = new ArrayList<String>();
	            try {
					projects = pxml.getCharValueForANode(xmlString,"Name","Id");
					request.setAttribute("AllProjects", projects);
					inc = 2;
					projectsView.include(request, response);																																																																																																																																																																																																																																																																																																																																																												
				} catch (ParserConfigurationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SAXException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	            outPrintWriter.close();
	            in.close();
            }
            else if(inc == 2){
            	int var = 1;
            	URL oracle = new URL("http://localhost:8090/test/ProjectController?projectaction=detailview&param_name="+var);
	            URLConnection yc = oracle.openConnection();
	            yc.setDoOutput(true);
	            BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
	            String inputLine = "",xmlString = "";
	            
	            //OutputStreamWriter out = new OutputStreamWriter(yc.getOutputStream());
	            while ((inputLine = in.readLine()) != null){
	            	xmlString += inputLine;
	            }
	            ParseXML pxml = new ParseXML();
	            ArrayList<String> pd = new ArrayList<String>();
	            try {
					pd = pxml.getCharValueForANode(xmlString,"PrjectId", "PrjectName","PrjectDesc","PrjectCatog","comment");
					request.setAttribute("ProjectDetail", pd);
					inc = 1;
					projectView.include(request, response);																																																																																																																																																																																																																																																																																																																																																												
				} catch (ParserConfigurationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SAXException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	            outPrintWriter.close();
	            in.close();
            }
            else if(inc == 3){
            	int var = 1;
            	
            	System.out.println(request.getParameter("comment"));
            	/*URL oracle = new URL("http://localhost:8090/test/ProjectController?projectaction=comment&param_name="+var);
	            URLConnection yc = oracle.openConnection();
	            yc.setDoOutput(true);
	            BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
	            String inputLine = "",xmlString = "";
	            
	            //OutputStreamWriter out = new OutputStreamWriter(yc.getOutputStream());
	            while ((inputLine = in.readLine()) != null){
	            	xmlString += inputLine;
	            }
	            ParseXML pxml = new ParseXML();
	            ArrayList<String> pd = new ArrayList<String>();
	            try {
					pd = pxml.getCharValueForANode(xmlString,"PrjectId", "PrjectName","PrjectDesc","PrjectCatog","comment");
					request.setAttribute("ProjectDetail", pd);
					inc = 3;
					projectView.include(request, response);																																																																																																																																																																																																																																																																																																																																																												
				} catch (ParserConfigurationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SAXException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	            outPrintWriter.close();
	            in.close();
            	*/
            }
        } else {
        	helpView.include( request, response );
        }
    }

    protected void doHelp( RenderRequest request, RenderResponse response )
        throws PortletException, IOException {

        helpView.include( request, response );

    }

    public void init( PortletConfig config ) throws PortletException {
        super.init( config );
        projectsView = config.getPortletContext().getRequestDispatcher( ALL_PROJECTS );
        projectView = config.getPortletContext().getRequestDispatcher( PROJECT_DETAIL );
        helpView = config.getPortletContext().getRequestDispatcher( HELP_VIEW );
    }

    public void destroy() {
        projectsView = null;
        projectView = null;
        helpView = null;
        super.destroy();
    }

}
